package com.house.controller.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.house.bean.HUser;
import com.house.service.HUserService;
import com.house.utils.ChangeTime;
import com.house.utils.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class UserController {
    @Autowired
    private HUserService hUserService;
    /**
     * 用户登录
     */
    @RequestMapping(value = "/user/login" ,method = RequestMethod.POST)
    public Object adminLogin(@RequestBody HUser hUser, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        HUser one = hUserService.getOne(new QueryWrapper<HUser>().eq("username", hUser.getUsername()).eq("password", hUser.getPassword()));
        if (hUser.getUsername()==null||"".equals(hUser.getUsername())){//?username==null||username.equals("")
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"用户名不能为空");
            return jsonObject;
        }
        if (hUser.getPassword()==null||"".equals(hUser.getPassword())){//?
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"密码不能为空");
            return jsonObject;
        }
        if(one!=null){
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"登录成功");
            jsonObject.put("userInfo",one);
            return jsonObject;
        }
        jsonObject.put(Consts.CODE,0);
        jsonObject.put(Consts.MSG,"用户名或密码错误");
        return jsonObject;
    }
    /**
     * 用户注册
     */
    @RequestMapping(value = "/user/add" , method = RequestMethod.POST)
    public Object addConsumer(@RequestBody  HUser hUser) throws ParseException {
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        Date nowTime = new Date();
        String now = sdf.format(nowTime);
        hUser.setCreateTime(ChangeTime.StringChangeTime(now));
        hUser.setHeadPhoto("/user.jpg");
        return hUserService.save(hUser);
    }
    /**
     * 根据用户id 修改用户密码
     */
    @RequestMapping(value = "/user/update_password",method = RequestMethod.POST)
    public Object updatePassword(@RequestParam("id") int id,@RequestParam("password") String password){
        HUser hUser = new HUser();
        System.out.println(id);
        System.out.println(password);
        hUser.setPassword(password);
        hUser.setId(id);
        return  hUserService.updateById(hUser);
    }
    /**
     * 根据用户id 修改用户信息
     */
    @RequestMapping(value = "/user/update_info",method = RequestMethod.POST)
    public Object updatePassword(@RequestParam("id") int id,@RequestParam("phone") String phone,@RequestParam("username")String username,@RequestParam("address") String address){
        HUser hUser = new HUser();
        hUser.setPassword(phone);
        hUser.setPassword(username);
        hUser.setPassword(address);
        hUser.setId(id);
        return  hUserService.updateById(hUser);
    }


}
